package com.luwanglin.oss.test1;

import java.util.ArrayList;
import java.util.UUID;

public class T {
    private static String arrayCopyToString(UUID uuid) {
        char[] dest = new char[32];
        char[] src = uuid.toString().toCharArray();

        System.arraycopy(src, 0, dest, 0, 8);
        System.arraycopy(src, 9, dest, 8, 4);
        System.arraycopy(src, 14, dest, 12, 4);
        System.arraycopy(src, 19, dest, 16, 4);
        System.arraycopy(src, 24, dest, 20, 12);

        return new String(dest);
    }

    private static String directlyToString(UUID uuid) {
        return Long.toHexString(uuid.getMostSignificantBits()) + Long.toHexString(uuid.getLeastSignificantBits());
    }

    private static String forToString(UUID uuid) {
        char[] src = uuid.toString().toCharArray();
        char[] chars = new char[32];

        for (int i = 0, j = 0; i < src.length; i++) {
            char c = src[i];

            if ('-' != c) {
                chars[j++] = c;
            }
        }

        return new String(chars);
    }

    public static void main(String[] args) {
        final int SIZE = 1000000;
        ArrayList<UUID> first = new ArrayList<>();

        for (int i = 0; i < SIZE; i++) {
            first.add(UUID.randomUUID());
        }

        long l1 = System.currentTimeMillis();

        first.forEach(T::directlyToString);

        long l2 = System.currentTimeMillis();

        first.forEach(T::arrayCopyToString);

        long l3 = System.currentTimeMillis();

        first.forEach(T::subStringToString);

        long l4 = System.currentTimeMillis();

        first.forEach(T::forToString);

        long l5 = System.currentTimeMillis();

        first.forEach(T::replaceToString);

        long l6 = System.currentTimeMillis();
        UUID uuid = UUID.randomUUID();

        System.out.println(uuid.toString());
        System.out.println(directlyToString(uuid));
        System.out.println(arrayCopyToString(uuid));
        System.out.println(subStringToString(uuid));
        System.out.println(forToString(uuid));
        System.out.println(replaceToString(uuid));
        System.out.println(l2 - l1);
        System.out.println(l3 - l2);
        System.out.println(l4 - l3);
        System.out.println(l5 - l4);
        System.out.println(l6 - l5);
    }

    private static String replaceToString(UUID uuid) {
        return uuid.toString().replaceAll("-", "");
    }

    private static String subStringToString(UUID uuid) {
        String src = uuid.toString();

        return src.substring(0, 8) + src.substring(9, 13) + src.substring(14,
                18) + src.substring(19,
                23) + src.substring(24,
                36);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
